<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/1
 * Time: 11:22
 */
class CashModel
{
    public function __construct()
    {
        $this->db = Yaf_Registry::get('db');
    }

    //根据条件查询资产动态信息
    public function selectAsset($where,$page, $pagesize = 20,$mywhere)
    {
        $wheresql="where a.type=2 and a.user_id=u.user_id  ";
        if(is_array($where)){
            foreach($where as $k=>$v){
                if($v){
                    if($v<>"%%"){
                        $wheresql.="and $k='".$v."' ";
                    }

                }
            }
        }
        $wheresql=str_replace(" like=", " like ", $wheresql);
        $page = $page < 1 ? 1 : $page;
        $myoffset = ($page - 1) * $pagesize;
        $sql = "select * from asset as a,user as u $wheresql $mywhere limit $myoffset,$pagesize";

        //print_r($sql);
        return $this->db->query($sql);
    }

    /* author dichaofeng
     * 分页显示
     * 获取资产总共条数
     */
    public function selectCount($where,$mywhere)
    {
        $wheresql="where a.type=2 and a.user_id=u.user_id  ";
        if(is_array($where)){
            foreach($where as $k=>$v){

                if($v){
                    if($v<>"%%"){
                        $wheresql.="and $k='".$v."' ";
                    }

                }
            }
        }
        $wheresql=str_replace(" like=", " like ", $wheresql);
        $sql = "select count(*) as count from asset as a,user as u $wheresql $mywhere";

        $result = $this->db->get_one($sql);
        return (int)(isset($result['count']) ? $result['count'] : 0);
    }
    /* author dichaofeng
     * 插入
     * 操作
     */
    public function setCash($data,$where,$table="asset")
    {

        if(is_array($data)){
            $sqldata="";
            $i=0;
            foreach($data as $k=>$v){
                if($i==count($data)-1){
                    $sqldata.="$k='".$v."'";
                }else{
                    $sqldata.="$k='".$v."',";
                }
                $i++;
            }
        }
        $wheresql="where 1  ";
        if(is_array($where)){
            foreach($where as $k=>$v){
                if($v){
                    if($v<>"%%"){
                        $wheresql.="and $k='".$v."' ";
                    }

                }
            }
        }
        $sql="update $table set $sqldata $wheresql";
        $result = $this->db->get_one($sql);
        return $result;

    }



} 